package com.andnnl.ip;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by chenss on 2024/12/3.
 */
public class IpAddressTest {
    public static void main(String[] args) {
//        test1();
        test_all();
    }

    private static void test_all() {
        try {
            // 获取所有网络接口
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();

                // 获取网络接口的所有IP地址
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();

                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();

                    // 过滤掉IPv6地址和回环地址
                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof java.net.Inet4Address) {
                        System.out.println("网络接口: " + networkInterface.getDisplayName());
                        System.out.println("IP地址: " + inetAddress.getHostAddress());

                        isInAnySubnet(inetAddress.getHostAddress());
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    private static void test1() {
        try {
            // 获取本机InetAddress对象
            InetAddress inetAddress = InetAddress.getLocalHost();

            // 获取本机IP地址
            String ipAddress = inetAddress.getHostAddress();

            // 获取本机主机名
            String hostName = inetAddress.getHostName();

            System.out.println("本机IP地址: " + ipAddress);
            System.out.println("本机主机名: " + hostName);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    public static boolean isInAnySubnet(String ip) {
        List<String> subnets = new ArrayList<>();
        subnets.add("192.168.4.0/24");
        subnets.add("122.118.1.0/24"); // 多个网段
        boolean isInAnySubnet = isInAnySubnet(ip, subnets);

        System.out.println("IP地址 " + ip + " 是否在任何网段内: " + isInAnySubnet);
        return isInAnySubnet;
    }
    public static boolean isInAnySubnet(String ip, List<String> subnets) {
        for (String subnet : subnets) {
            String[] parts = subnet.split("/");
            String subnetAddress = parts[0];
            int subnetMask = Integer.parseInt(parts[1]);

            if (isInSubnet(ip, subnetAddress, subnetMask)) {
                return true;
            }
        }

        return false;
    }

    public static boolean isInSubnet(String ip, String subnet, int subnetMask) {
        String[] ipParts = ip.split("\\.");
        String[] subnetParts = subnet.split("\\.");

        int mask = 0xffffffff << (32 - subnetMask);

        for (int i = 0; i < 4; i++) {
            int ipPart = Integer.parseInt(ipParts[i]);
            int subnetPart = Integer.parseInt(subnetParts[i]);

            if ((ipPart & mask) != (subnetPart & mask)) {
                return false;
            }
        }

        return true;
    }
}
